<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Factories\HasFactory;
use Illuminate\Database\Eloquent\Model;
use Illuminate\Database\Eloquent\SoftDeletes;

class Coupons extends Model
{
    use HasFactory;
    use SoftDeletes;
    protected $fillable = [
        'member_id',
        'name',
        'address',
        'cover',
        'type',
        'type_value',
        'time_type',
        'time',
        'detail',
        'status'
    ];

    protected $appends = ['type_str','value','time_str'];

    protected $casts = [
        'type_value' => 'array',
    ];

    public function getCoverAttribute($_value)
    {
        if($_value) {
            return cool_media(Media::where('id',$_value)->value('thumb'));
        }
    }

    /**
     * 字符串卡券类型
     * @param $_value
     * @param $_data
     * @return mixed
     */
    public function getTypeStrAttribute()
    {
        $_type = "";
        switch ($this->type) {
            case 'dj':
                $_type = '代金券';
                break;
            case 'mj':
                $_type = '满减券';
                break;
            case 'dh':
                $_type = '兑换券';
                break;
            case 'zk':
                $_type = '折扣券';
                break;
            case 'rc':
                $_type = '入场券';
                break;
        }
        return $_type;
    }

    /**
     * 卡券状态
     */
    public function getStatusAttribute($_value)
    {
        $_status = $_value;
        if($this->time_type == 2 && $this->time+86400-1 < time()) {
            $_status = 8;
        }
        return (int) $_status;
    }

    /**
     * 卡券value值
     * @return mixed
     */
    public function getValueAttribute()
    {
        $_value = "";
        $_type_value = $this->type_value;
        switch($this->type) {
            case 'dj':
                $_value = $_type_value["value"]."元";
                break;
            case 'mj':
                $_value = "满".$_type_value["face"]."元，减".$_type_value["value"]."元";
                break;
            case 'zk':
                $_value = $_type_value["value"]."折";
                break;
            case 'dh':
            case 'rc':
                $_value = $_type_value["value"];
                break;
        }
        unset($_type_value);
        return $_value;
    }

    /**
     * 获取卡券到期时间字符串
     * @return string
     */
    public function getTimeStrAttribute()
    {
        $_time_str = "";
        switch ($this->time_type) {
            case "1":
                $_time_str = "永久有效";
                break;
            case "2":
                $_time_str = "卡券有效期至".date("Y-m-d",$this->time);
                break;
            case "3":
                $_time_str = "领券后".$this->time."天内有效";
                break;
        }
        return $_time_str;
    }

    /**
     * 处理卡券说明规则
     * @param $_value
     * @return string
     */
    public function getDetailAttribute($_value)
    {
        return htmlspecialchars_decode($_value);
    }


    /**
     * 设置卡券状态
     */
    public function setCoverAttribute($_value)
    {
        if($_value){
            return $this->attributes['cover'] = $_value[0];
        } else {
            return null;
        }
    }

    /**
     * 用户信息
     */
    public function member()
    {
        return $this->hasOne(Member::class,'id','member_id');
    }
}
